package xyz.zdsoft.homework_manage_module.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.zdsoft.common.entity.Attachement;
import xyz.zdsoft.common.entity.SysUser;
import xyz.zdsoft.common.entity.WorkInfo;
import xyz.zdsoft.common.entity.WorkSubmitInfo;
import xyz.zdsoft.common.service.IAttachementService;
import xyz.zdsoft.common.service.ISysUserService;
import xyz.zdsoft.common.service.IWorkSubmitInfoService;
import xyz.zdsoft.common.utils.JwtUtils;
import xyz.zdsoft.common.utils.Result;
import xyz.zdsoft.homework_manage_module.Service.TeacherOperationService;
import xyz.zdsoft.homework_manage_module.bo.QueryHomework;
import xyz.zdsoft.homework_manage_module.mapper.TeacherOperationMapper;
import xyz.zdsoft.homework_manage_module.vo.RestEnclosure;
import xyz.zdsoft.homework_manage_module.vo.RestPerTask;
import xyz.zdsoft.homework_manage_module.vo.RestQueryHomework;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherOperationServiceImpl implements TeacherOperationService {
    @Autowired
    HttpServletRequest request;
    @Autowired
    ISysUserService iSysUserService;
    @Autowired
    IWorkSubmitInfoService iWorkSubmitInfoService;
    @Autowired
    TeacherOperationMapper mapper;

    /*查询作业*/
    @Override
    public Result QueryHomework(QueryHomework queryHomework) {
        String teacher_id= JwtUtils.getMemberIdByJwtToken(request);//获取教师id
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",teacher_id);
        String  teacherName=iSysUserService.getOne(queryWrapper).getName();
        String type=null;
        String types=null;
        if (queryHomework.getCurrent()<=0||queryHomework.getCurrent()==null){
            queryHomework.setCurrent(1);
        }
        if (queryHomework.getSize()<=0||queryHomework.getSize()==null){
            queryHomework.setSize(10);
        }
        if (queryHomework.getTypes().equals("0") ||queryHomework.getTypes()==null||queryHomework.getTypes()==""){
            type="未批复";
        }else {
            types="已批复";
        }
        Page page= PageHelper.startPage(queryHomework.getCurrent(),queryHomework.getSize(),"id  DESC");
        List<RestQueryHomework> list=mapper.QueryHomework(queryHomework,teacherName,type,types);
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setPages(page.getPages());//总页数
        pageInfo.setTotal(page.getTotal());//总条数
        for (int i=0;i<list.size();i++){
            if(list.get(i).getWorkType().equals("0")){
                list.get(i).setWorkType("平时作业");
            }else if(list.get(i).getWorkType().equals("1")){
                list.get(i).setWorkType("阶段性作业");
            }else {
                list.get(i).setWorkType("最终交互作业");
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("page",page.getPages());
        map.put("total",page.getTotal());
        map.put("info",list);
        return Result.ok().data(map);
    }
     /*作业任务*/
    @Override
    public Result<RestPerTask> PersonalTask(Integer id) {
        RestPerTask restPerTask=mapper.PersonalTask(id);
        //获取作业id
        QueryWrapper<WorkSubmitInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Long  work_id=iWorkSubmitInfoService.getOne(queryWrapper).getWorkId();
        //获取附件
        List<RestEnclosure> list=mapper.Enclosure(work_id,"0");
        if (list!=null  && list.size()>0){
            restPerTask.setRestEnclosures(list);
        }
        return Result.ok().data(restPerTask);
    }

    /*学生答题*/
    @Override
    public Result StudentAnswers(Integer id) {
        QueryWrapper<WorkSubmitInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        String  answerRemark=iWorkSubmitInfoService.getOne(queryWrapper).getAnswerRemark();
        //获取附件
        List<RestEnclosure> list=mapper.Enclosure(id.longValue(),"1");
        Map<String,Object> map = new HashMap<>();
        map.put("answerRemark",answerRemark);
        map.put("info",list);
        return Result.ok().data(map);
    }

    /*老师批复*/
    @Override
    public Result ApprovalWork(Integer id,float score, String teacher_remark) {
        if (teacher_remark==null || teacher_remark ==""){
            return Result.error().message("请输入评语");
        }
        boolean b= mapper.ApprovalWork(id,score,teacher_remark);
        if (b==true){
            return Result.ok().data("批复成功");
        }
        return Result.error().message("批复失败");
    }
    /*查看老师批复*/
    @Override
    public Result ConfirmSubmission(Integer id) {
        QueryWrapper<WorkSubmitInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        String  teacherRemark=iWorkSubmitInfoService.getOne(queryWrapper).getTeacherRemark();
        BigDecimal score=  iWorkSubmitInfoService.getOne(queryWrapper).getScore();
        //获取附件
        List<RestEnclosure> list=mapper.Enclosure(id.longValue(),"3");
        Map<String,Object> map = new HashMap<>();
        map.put("teacherRemark",teacherRemark);
        map.put("score",score);
        map.put("info",list);
        return Result.ok().data(map);
    }


}
